<template>
  <div>
    <!-- 流程确认节点确认窗口 -->
    <div v-for="item in showConfirmNodeParams.dialogArr" :key="item.dialogId">
      <show-confirm-node v-if="item.visible" :params="item" />
    </div>
  </div>
</template>

<script>
export default {
  components: {
    ShowConfirmNode: () => import('@/views/flow/dialog/ShowConfirmNode.vue')
  },
  data() {
    return {
      // 流程确认节点确认窗口  注意：因为可能同时存在多个一样的弹窗，所以这里需要将弹窗的obj用key进行保存
      showConfirmNodeParams: {
        dialogArr: []
        // 唯一的key: {
        //   visible: false,
        //   confirm: () => {
        //   ...
        //   },
        //   参数1:'',
        //   参数2:'',
        //   ...
        // }
      }
    }
  },
  created() {
    // 将主页消息推送组件保存到root组件去，因为子组件可能会用到
    this.$root.AomMainMessage = this
    this.initReg()
    window.m = this
  },
  methods: {
    // 从弹窗数组里面找指定ID的弹窗
    getDialogById(dialogParams, id) {
      const arr = dialogParams.dialogArr
      for (let i = 0; i < arr.length; i++) {
        if (arr[i].dialogId === id) {
          return arr[i]
        }
      }
      return null
    },
    destroyDialog(dialogParams, dialog) {
      const i = dialogParams.dialogArr.indexOf(dialog)
      dialogParams.dialogArr.slice(i, 1)
    },
    initReg() {
      const c = this.Commonmsg
      const msg = [
        // c.WM_IOCP_CONNECT,
        // 交互函数的等待弹窗
        c.WM_WAITINPUT,
        // //设置双机互备地址时 //获取双机互备数据，解决首次配置页面后断开Server，地址为空问题。
        c.WM_BACKUP_SERVER,
        c.WM_BACKUP_SWITCHB, // 未用到
        c.WM_BACKUP_SWITCHM, // 未用到
        c.WM_DISTRIBUTE_SERVER, // 未用到
        // 判断当前用户是否有权限接收
        c.WM_FORUM_MESSAGE_NEW,
        c.WM_FORUM_MESSAGE_READ,
        c.WM_FORUM_MESSAGE_DEL,
        c.WM_FLOW_CONFIRM,
        // 流程节点确认
        c.WM_FLOW_CONFIRMOBJ,
        c.WM_ESSENCE_MENU_ADD, // 未用到
        c.WM_ESSENCE_MENU_DEL // 未用到
      ]
      this.Global.mainMessagesReg(new this.CMainMessages(this), msg, false)
    },
    CMainMessages(vueCom) {
      const c = vueCom.Commonmsg
      this.WndProc = (msg, vPar) => {
        switch (msg) {
          // 流程确认节点弹窗
          case c.WM_FLOW_CONFIRMOBJ: {
            vueCom.msg_WM_FLOW_CONFIRMOBJ(vPar)
            break
          }
        }
      }
    },
    // 流程确认节点弹窗
    msg_WM_FLOW_CONFIRMOBJ(vPar) {
      const user = sessionStorage.getItem('user')
      // 判断当前用户是否应该弹出窗口
      const confirmIds = vPar.getStr('ConfirmUserID') || ''
      if (confirmIds.indexOf(`'${user}'`) === -1) return
      // 获取当前确认规则配置
      const str = this.Global.getSystemConfigByName(
        this,
        '流程确认节点配置'
      ).value
      let value = 1 // 默认值
      if (/^\{\d\}/.test(str)) {
        value = str.substring(1, 2)
      }
      if (
        (!(vPar.getStr('User') && vPar.getStr('Pass')) &&
          !vPar.getBool('OpenFlow') /* 仅打开流程时弹出*/ &&
          (value === '1' /* 自动弹出所有*/ ||
            (vPar.getBool('R_RedoObject') &&
              value === '0'))) /* 双击单出所有*/ ||
        (value === '2' /* 双击弹出自己*/ &&
          sessionStorage.getItem('loginID') === vPar.getStr('R_RedoLoginID'))
      ) {
        this.show_WM_FLOW_CONFIRMOBJ(vPar)
      }
    },
    // 显示确认节点弹窗， 因为其余组件可能会调用这个方法，所以拆开来写
    show_WM_FLOW_CONFIRMOBJ(vPar) {
      const id = vPar.getStr('ObjectID')
      const showConfirmNodeParams = this.showConfirmNodeParams
      let dialog = this.getDialogById(showConfirmNodeParams, id) // 考虑多个弹窗
      let isNew = false
      if (!dialog) {
        dialog = {}
        isNew = true
      }
      dialog.vTmp = vPar
      dialog.dialogId = id
      dialog.isFlowNodeConfrimMsg = true
      dialog.title = `${vPar.getStr('FlowName')}(${vPar.getStr('ObjectText')})`
      dialog.confirm = () => {
        dialog.visible = false
        this.destroyDialog(showConfirmNodeParams, dialog)
      }
      dialog.visible = true
      // 增加必须放在后面否则无法监听到
      if (isNew) showConfirmNodeParams.dialogArr.push(dialog)
    }
  }
}
</script>

<style>
</style>
